package com.itstyle.seckill.util.statusflow;

import com.google.common.collect.Lists;
import org.springframework.aop.support.AopUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author lifujun
 * @description: 订单按钮处理类
 * @date 2023/9/5 16:13
 */
@Service
public class OrderButtonGenService {

    @Autowired
    private ApplicationContext applicationContext;

    /**
     * 存储订单事件响应方法体
     */
    private final Map<BigOrderClassificationEnum, Map<BigOrderStatusEnum, Method>> methodMap = new HashMap<>();
    /**
     * 存储订单类型对应的状态机Service类
     */
    private final Map<BigOrderClassificationEnum, Object> serviceMap = new HashMap<>();

    @PostConstruct
    public void init(){
        Map<String, Object> beansWithAnnotation = applicationContext.getBeansWithAnnotation(BigOrderButtonClassificationType.class);
        for (Map.Entry<String, Object> entry : beansWithAnnotation.entrySet()) {

            Class<?> targetClass = AopUtils.getTargetClass(entry.getValue());

            BigOrderButtonClassificationType mallOrderMachine = targetClass.getAnnotation(BigOrderButtonClassificationType.class);
            Method[] methods = targetClass.getMethods();
            if(mallOrderMachine!=null&&mallOrderMachine.value()!=null){
                for (BigOrderClassificationEnum orderClassificationEnum : mallOrderMachine.value()) {
                    serviceMap.put(orderClassificationEnum, entry.getValue());
                    Map<BigOrderStatusEnum, Method> map = new HashMap<>();
                    for (Method method : methods) {
                        if(method.isAnnotationPresent(BigOrderButtonStatusType.class)){
                            BigOrderButtonStatusType orderStatusType = method.getAnnotation(BigOrderButtonStatusType.class);
                            if(orderStatusType!=null&&orderStatusType.value()!=null){
                                for (BigOrderStatusEnum orderStatusEnum : orderStatusType.value()) {
                                    map.put(orderStatusEnum,method);
                                }
                            }

                        }
                    }
                    methodMap.put(orderClassificationEnum,map);
                }
            }
        }


        List<OrderButtonVo> orderButtonVos = this.genButtonList(BigOrderClassificationEnum.create("1"), BigOrderStatusEnum.create("901"));
        System.out.println(orderButtonVos);


    }

    public List<OrderButtonVo> genButtonList(BigOrderClassificationEnum classificationEnum,BigOrderStatusEnum statusEnum,Object data) {
        try {
            if(classificationEnum == null || statusEnum == null ){
                return Lists.newArrayList();
            }
            //反射调用事件方法
            Object[] args = new Object[]{data};
            Object invoke = methodMap.get(classificationEnum).get(statusEnum).invoke(serviceMap.get(classificationEnum), args);
            return (List<OrderButtonVo>) invoke;
        }catch (Exception e){
            return Lists.newArrayList();
        }

    }

    public List<OrderButtonVo> genButtonList(BigOrderClassificationEnum classificationEnum,BigOrderStatusEnum statusEnum) {
        try {
            if(classificationEnum == null || statusEnum == null ){
                return Lists.newArrayList();
            }
            //反射调用事件方法
            Object[] args = new Object[]{null};
            Object invoke = methodMap.get(classificationEnum).get(statusEnum).invoke(serviceMap.get(classificationEnum),args);
            return (List<OrderButtonVo>) invoke;
        }catch (Exception e){
            return Lists.newArrayList();
        }

    }

}
